package m.jclecai.com.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;

import com.handmark.pulltorefresh.library.xrecyclerview.ProgressStyle;
import com.handmark.pulltorefresh.library.xrecyclerview.XRecyclerView;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.adapter.NoticeAdapter;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.Response.ResNewsList;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

/**
 * Created by ZD on 2017/7/21.
 * 公告
 */

public class NoticeActivity extends BaseActivity {
    @BindView(R.id.xlvList)
    XRecyclerView xlvList;
    NoticeAdapter adapter;

    @Override
    protected int getContentView() {
        return R.layout.activity_notice;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("公告");
        leftOptionText("");
        LinearLayoutManager lManager = new LinearLayoutManager(this);
        lManager.setOrientation(LinearLayoutManager.VERTICAL);
        xlvList.setLayoutManager(lManager);
        xlvList.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        xlvList.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);

        adapter = new NoticeAdapter(this,new ArrayList<ResNewsList.NewsBean>());
        xlvList.setAdapter(adapter);
        xlvList.setNoMore(false);
        xlvList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //refresh data here
                xlvList.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                // load more data here
                xlvList.loadMoreComplete();
            }
        });

        adapter.setListener(new NoticeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                ResNewsList.NewsBean newsBean = adapter.getData().get(position);
                String url= "http://m.yuecp.net/view/index.html?id="+newsBean.getId();
                startActivity(WebHelperActivity.getInstance(NoticeActivity.this,url,false,newsBean.getTitle()));
            }
        });

    }

    @Override
    protected void initData() {
        super.initData();
        //这里我们请求服务器获取数据
        requestData();
    }

    private void requestData() {
        HashMap<String, String> params = new HashMap<>();
        HttpManager.postAsyn(Constant_Url.NEWS_LIST, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                DisPlay("网络异常！！！");
            }

            @Override
            public void onResponse(String response) {

                if(!TextUtils.isEmpty(response)){

                    ResNewsList resNewsList = FastOk.get(response, ResNewsList.class);
                    if ("0".equals(resNewsList.getStatus())) {

                        setData(resNewsList.getRows());
                    }else{
                        DisPlay(resNewsList.getMessage());
                    }

                }else{
                    //这里表示返回为空；
                    DisPlay("网络异常！！！");
                }

            }
        },params);
    }

    private void setData(List<ResNewsList.NewsBean> rows) {
            adapter.refreshData(rows);
    }


}
